package com.exrobot.module.rbt.service.robotappversion;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.robotappversion.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotappversion.RobotAppVersionDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.robotappversion.RobotAppVersionMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.framework.common.util.collection.CollectionUtils.diffList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 机器人APP版本 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RobotAppVersionServiceImpl implements RobotAppVersionService {

    @Resource
    private RobotAppVersionMapper robotAppVersionMapper;

    @Override
    public Long createRobotAppVersion(RobotAppVersionSaveReqVO createReqVO) {
        // 插入
        RobotAppVersionDO robotAppVersion = BeanUtils.toBean(createReqVO, RobotAppVersionDO.class);
        robotAppVersionMapper.insert(robotAppVersion);

        // 返回
        return robotAppVersion.getId();
    }

    @Override
    public void updateRobotAppVersion(RobotAppVersionSaveReqVO updateReqVO) {
        // 校验存在
        validateRobotAppVersionExists(updateReqVO.getId());
        // 更新
        RobotAppVersionDO updateObj = BeanUtils.toBean(updateReqVO, RobotAppVersionDO.class);
        robotAppVersionMapper.updateById(updateObj);
    }

    @Override
    public void deleteRobotAppVersion(Long id) {
        // 校验存在
        validateRobotAppVersionExists(id);
        // 删除
        robotAppVersionMapper.deleteById(id);
    }

    @Override
        public void deleteRobotAppVersionListByIds(List<Long> ids) {
        // 删除
        robotAppVersionMapper.deleteByIds(ids);
        }


    private void validateRobotAppVersionExists(Long id) {
        if (robotAppVersionMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public RobotAppVersionDO getRobotAppVersion(Long id) {
        return robotAppVersionMapper.selectById(id);
    }

    @Override
    public PageResult<RobotAppVersionDO> getRobotAppVersionPage(RobotAppVersionPageReqVO pageReqVO) {
        return robotAppVersionMapper.selectPage(pageReqVO);
    }

    @Override
    public RobotAppVersionDO getRobotAppLastVersion() {
        LambdaQueryWrapper<RobotAppVersionDO> queryWrapper = new LambdaQueryWrapper<RobotAppVersionDO>();
        queryWrapper.last("order by version_code asc limit 1 ");
        return robotAppVersionMapper.selectOne(queryWrapper);
    }

}